package teacher;

import java.util.*;

public class Poker {
    Card[] cards;
    String[] suits = {"红桃","黑桃","方片","草花"};
    public Poker(){
        cards = new Card[20];
        int index = 0;
        for(int i = 0; i < suits.length; i++){
            for(int j = 10; j <= 14; j++){
                Card card = new Card(suits[i],j);
                cards[index++] = card;
            }
        }
    }

    public void outputCard(){
        int index = 0;
        for(int i = 0; i < suits.length; i++){
            for(int j = 10; j <= 14; j++){
                System.out.print(cards[index++]+" ");
            }
            System.out.println();
        }
    }

    public void shuffle(){
        Random random = new Random();
        for(int i = 0; i < 20; i++){
            int randomIndex = random.nextInt(20);
            Card tmp = cards[i];
            cards[i] = cards[randomIndex];
            cards[randomIndex] = tmp;
        }
    }

    public List<Card> getOneHand(){
        List<Card> hands = new ArrayList<>();
        this.shuffle();
        for(int i = 0; i < 5; i++){
            hands.add(cards[i]);
        }
        return hands;
    }

    public static void main(String[] args) {
        Poker poker = new Poker();
        poker.outputCard();
        poker.shuffle();
        System.out.println();
        poker.outputCard();

        System.out.println();
        List<Card> hand = poker.getOneHand();
        System.out.println(hand);
        String handType = poker.getHandType(hand);
        System.out.println(handType);
    }

    public Card getOneCard(int index){
        return cards[index];
    }

    public String getHandType(List<Card> hand){
        Set<String> suitSet = new HashSet<>();
        Set<Integer> rankSet = new HashSet<>();
        List<Integer> rankLst = new ArrayList<>();
        Map<Integer,Integer> countMap = new HashMap<>();
        for(Card card : hand){
            suitSet.add(card.suit);
            rankSet.add(card.rank);
            rankLst.add(card.rank);

            if(countMap.containsKey(card.rank)){
                int count = countMap.get(card.rank);
                count++;
                countMap.put(card.rank,count);
            }
            else{
                countMap.put(card.rank,1);
            }
        }

        Collections.sort(rankLst);

        boolean bIsStraight = false;
        boolean bIsSameColor = false;

        if(suitSet.size() == 1){
            bIsSameColor = true;
//            return "同花";
        }
        if(rankSet.size() == 5 && rankLst.get(4) - rankLst.get(0) == 4){
            bIsStraight = true;
            //            return "顺子";
        }
        if(bIsSameColor && bIsStraight){
            return "同花顺";
        }
        else if(bIsSameColor){
            return "同花";
        }
        else if(bIsStraight){
            return "顺子";
        }

        if(rankSet.size() == 4){
            return "一对";
        }
        if(rankSet.size() == 5){
            return "杂牌";
        }

        if(rankSet.size() == 2){
            //4带1or3带2
            if(countMap.containsValue(4)){
                return "四条";
            }
            else{
                return "满堂红";
            }
        }
        if(rankSet.size() == 3){
            //311or221
            if(countMap.containsValue(3)){
                return "三条";
            }
            else{
                return "两对";
            }
        }
        return "unknown";
    }
}
